package com.todod.base.module.infra.service.basecarousel;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.todod.base.module.infra.controller.admin.basecarousel.vo.*;
import com.todod.base.module.infra.dal.dataobject.basecarousel.BaseCarouselDO;
import com.todod.base.framework.common.pojo.PageResult;
import com.todod.base.framework.common.pojo.PageParam;
import com.todod.base.framework.common.util.object.BeanUtils;

import com.todod.base.module.infra.dal.mysql.basecarousel.BaseCarouselMapper;

import static com.todod.base.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.todod.base.module.infra.enums.ErrorCodeConstants.*;

/**
 * 轮播图管理 Service 实现类
 *
 * @author 平台管理员
 */
@Service
@Validated
public class BaseCarouselServiceImpl implements BaseCarouselService {

    @Resource
    private BaseCarouselMapper baseCarouselMapper;

    @Override
    public Long createBaseCarousel(BaseCarouselSaveReqVO createReqVO) {
        // 插入
        BaseCarouselDO baseCarousel = BeanUtils.toBean(createReqVO, BaseCarouselDO.class);
        baseCarouselMapper.insert(baseCarousel);
        // 返回
        return baseCarousel.getId();
    }

    @Override
    public void updateBaseCarousel(BaseCarouselSaveReqVO updateReqVO) {
        // 校验存在
        validateBaseCarouselExists(updateReqVO.getId());
        // 更新
        BaseCarouselDO updateObj = BeanUtils.toBean(updateReqVO, BaseCarouselDO.class);
        baseCarouselMapper.updateById(updateObj);
    }

    @Override
    public void deleteBaseCarousel(Long id) {
        // 校验存在
        validateBaseCarouselExists(id);
        // 删除
        baseCarouselMapper.deleteById(id);
    }

    private void validateBaseCarouselExists(Long id) {
        if (baseCarouselMapper.selectById(id) == null) {
            throw exception(BASE_CAROUSEL_NOT_EXISTS);
        }
    }

    @Override
    public BaseCarouselDO getBaseCarousel(Long id) {
        return baseCarouselMapper.selectById(id);
    }

    @Override
    public PageResult<BaseCarouselDO> getBaseCarouselPage(BaseCarouselPageReqVO pageReqVO) {
        return baseCarouselMapper.selectPage(pageReqVO);
    }

}